package com.lk.springboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.springboot.bean.Buy;
import com.lk.springboot.dao.BuyMapper;
import com.lk.springboot.dao.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BuyController {
    @Autowired
    BuyMapper buyMapper;

    @RequestMapping("/listBuy")
    public String listBuy(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "15") int size) throws Exception {
        PageHelper.startPage(start, size, "buy_id desc");
        List<Buy> cs = buyMapper.findAll();
        PageInfo<Buy> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listBuy";
    }

    @RequestMapping("/searchBuy")
    public String searchList(String title, String u_name, Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "15") int size) throws Exception {
        PageHelper.startPage(start, size, "buy_id desc");
        List<Buy> cs = null;
        if (u_name != "") {
            cs = buyMapper.findByName(u_name);
        } else if (title != "") {
            cs = buyMapper.findByTitle(title);
        } else if (title == "" & u_name == "") {
            cs = buyMapper.findAll();
        }
        PageInfo<Buy> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listBuy";
    }
}
